//
//  ImageTable.m
//  ImageReplacer
//
//  Created by Tyler Powell on 5/1/12.
//  Copyright 2012 Georgia Institute of Technology. All rights reserved.
//

#import "ButtonImageTable.h"

@implementation ButtonImageTable

@synthesize imageView;
@synthesize writer;

- (int)numberOfRowsInTableView:(NSTableView*) tableView {
    //int count = countLines(@"enabledButtons.txt");
	//return count>0 ? count-1 : 0;
    return [[[tableView writer] enbuttons] count];
}
- (id)tableView:(NSTableView*) tableView
    objectValueForTableColumn:NSTableColumn
    row:(int)row{
    //NSMutableArray* marray = [NSMutableArray arrayWithArray:getLines(@"images.txt")];
    //[marray removeLastObject];
    //NSLog(@"Row: %d",row);
    /*
    NSArray* originalLines = getLines(@"enabledButtons.txt");
    NSMutableArray* mutableLines = [NSMutableArray arrayWithArray:originalLines];
    for (int i = 0; i<[originalLines count]; i++) {
        NSString* imgpath = [originalLines objectAtIndex:i];
        NSArray* components = [imgpath componentsSeparatedByString:@"/"];
        [mutableLines replaceObjectAtIndex:i withObject:[components lastObject]];
    }
	return[mutableLines objectAtIndex:row];
     */
    return[[[[tableView writer] enbuttons] objectAtIndex:row] lastPathComponent];
}
- (void) tableView:(NSTableView*)tableView
    setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSString* current = [[[tableView writer] enbuttons] objectAtIndex:row];
    NSString* currentFileType = [current pathExtension];
    NSString* newFileType = [object pathExtension];
    if(![currentFileType isEqualToString:newFileType])
        return;
    //NSString* file =[[current lastPathComponent]stringByDeletingPathExtension];
    NSString* newPath = [current stringByDeletingLastPathComponent];
    newPath = [newPath stringByAppendingString:@"/"];
    newPath = [newPath stringByAppendingString:object];
    if([[NSFileManager defaultManager] fileExistsAtPath:newPath])
        return;
    //NSURL* currentPath = [NSURL URLWithString:current];
    [[NSFileManager defaultManager] movePath:current toPath:newPath handler:nil];
    //writeToFileAtIndex(@"enabledButtons.txt", newPath, row);
    [[tableView writer] replaceEnabledButtonAtIndex:row withImage:newPath];
}
- (void)removeItem:(id)sender{
	id selection = [self selectedRowIndexes];
	int index = [selection firstIndex];
	//removeItemFromFile(@"enabledButtons.txt", index);
    [writer removeEnabledButton:index];
	[self reloadData];
    [imageView setSelectedImage:self];
}
- (void)refreshTable:(id)sender{
	[self reloadData];
	//[self display];
}
- (void)saveSelection:(id)sender{
	id selection = [self selectedRowIndexes];
	if([selection count]==0)
		return;
	int index = [selection firstIndex];
	//NSString* indexString = getItemFromFileAtIndex(@"enabledButtons.txt", index);
    NSString* indexString = [[writer enbuttons] objectAtIndex:index];
    //NSLog(indexString);
	//writeToFileAtIndex(@"options.txt", indexString,3);
    [writer replaceOptionAtIndex:3 withOption:indexString];
}

@end
